/*
 *  Copyright (c) 2009 Giuseppe Trisciuoglio
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.jquery4jsf.custom.accordion;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.jquery4jsf.javascript.JSElement;
import org.jquery4jsf.renderkit.JQueryBaseRenderer;
import org.jquery4jsf.renderkit.RendererUtilities;
import org.jquery4jsf.renderkit.html.HTML;
import org.jquery4jsf.utilities.MessageFactory;

public class AccordionSubPanelRenderer extends JQueryBaseRenderer {

	public static final String RENDERER_TYPE = "org.jquery4jsf.AccordionSubPanelRenderer";

	protected void encodeHeaderPanel(FacesContext context, AccordionSubPanel accordionSubPanel) throws IOException {
		ResponseWriter responseWriter = context.getResponseWriter();
        responseWriter.startElement(HTML.TAG_H3, accordionSubPanel);
        if (accordionSubPanel.getHeaderClass() != null)
        	responseWriter.writeAttribute("class", accordionSubPanel.getHeaderClass(), "headerClass");
        if (accordionSubPanel.getHeaderStyle() != null)
        	responseWriter.writeAttribute("style", accordionSubPanel.getHeaderStyle(), "headerStyle");
        responseWriter.startElement(HTML.TAG_A, accordionSubPanel);
        responseWriter.writeAttribute(HTML.HREF, "#", null);
        responseWriter.writeText(accordionSubPanel.getPanelName(), "panelName");
        responseWriter.endElement(HTML.TAG_A);
        responseWriter.endElement(HTML.TAG_H3);
	}
	
	protected void encodeContentPanel(FacesContext context, AccordionSubPanel accordionSubPanel) throws IOException{
        ResponseWriter responseWriter = context.getResponseWriter();
        responseWriter.startElement(HTML.TAG_DIV, accordionSubPanel);
        if (accordionSubPanel.getContentClass() != null)
        	responseWriter.writeAttribute("class", accordionSubPanel.getContentClass(), "contentClass");
        if (accordionSubPanel.getHeaderStyle() != null)
        	responseWriter.writeAttribute("style", accordionSubPanel.getContentStyle(), "contentStyle");
        RendererUtilities.renderChildren(context,accordionSubPanel);
        responseWriter.endElement(HTML.TAG_DIV);
	}
	
	public void encodeChildren(FacesContext context, UIComponent component) throws IOException {        
	}

	public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        if(context == null || component == null)
            throw new NullPointerException(MessageFactory.getMessage("com.sun.faces.NULL_PARAMETERS_ERROR"));
        if(!component.isRendered())
            return;
        AccordionSubPanel accordionSubPanel = null;
        if (component instanceof AccordionSubPanel) {
			accordionSubPanel = (AccordionSubPanel) component;
		}
        if (accordionSubPanel.getPanelName() == null){
        	throw new NullPointerException(MessageFactory.getMessage("com.sun.faces.NULL_PARAMETERS_ERROR"));
        }
        encodeHeaderPanel(context, accordionSubPanel);
        encodeContentPanel(context, accordionSubPanel);
	}

	public boolean getRendersChildren() {
		return true;
	}

	public JSElement getJSElement(FacesContext context, UIComponent component) {
		return null;
	}
}
